package cool.zhang.controller;

import cool.zhang.model.PayOrder;
import cool.zhang.repository.ABCTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chemeng on 2017/3/10.
 */
@RestController
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Resource(name = "aBCTest")
    private ABCTest aBCTest;

    @RequestMapping(value = {"/", "index"})
    public void home(HttpServletResponse response) throws IOException {
        logger.debug("HomeController.home");
        System.out.println(aBCTest.test());
        response.setHeader("Location", "lejia://result?success");
        //return "Hello Spring Boot!";
    }

    @RequestMapping("/down")
    public void testFileStream(HttpServletResponse response) {
        String filename = "test" + formatDate("yyyyMMdd", new Date()) + ".txt";

        try {
            filename = URLEncoder.encode(filename, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);

            StringBuilder writer = new StringBuilder();


            List<PayOrder> payOrderList = new ArrayList<>();

            for (int i = 0; i < 5; i++) {
                PayOrder payOrder = new PayOrder();

                payOrder.setPayOrderId(String.valueOf(i));
                payOrder.setAmount(new BigDecimal(888));
                payOrder.setOrderTime(new Date());

                payOrderList.add(payOrder);
            }
            for (PayOrder payOrder : payOrderList) {
                writer.append(payOrder.getPayOrderId() + "|");
                writer.append("0012|");
                writer.append(payOrder.getAmount().toString() + "|");
                writer.append(payOrder.getAmount().divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_DOWN)
                        .toString()
                        + "|");
                writer.append(formatDate("yyyyMMdd HH:mm:ss", payOrder.getOrderTime()));
                writer.append("\r\n");
            }
            FileCopyUtils.copy(writer.toString().getBytes(), response.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();

        }
    }


    private String formatDate(String format, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    @RequestMapping("/testAjax1")
    public String testAjax1(@RequestParam("username") String username, @RequestParam("password") String password) {
        return username + password;
    }

    @RequestMapping("/testAjax2")
    public String testAjax2(@RequestParam("username") String username, @RequestParam("password") String password) {
        return username + password;
    }
}
